const fs = require('fs');
const https = require('https');
const http = require('http');
//http://129.211.168.191:8080/mvoa/hrmStaffInfo/queryStaffDetail?userId=fafea7a7261840d2a01a137abb4cec1e
//http://129.211.168.191:8080/mvoa/img/appdata/avatar/68711050e4b00f929a22109c.jpg
// 读取 search.json 文件
const searchData = JSON.parse(fs.readFileSync('search.json', 'utf8'));
const ids = searchData.map(item => item.id);
const results = [];
let completedRequests = 0;

// 封装请求函数
function makeRequest(id) {
  const url = `http://129.211.168.191:8080/mvoa/hrmStaffInfo/queryStaffDetail?userId=${id}`;
  const protocol = url.startsWith('https') ? https : http;

  protocol.get(url, (res) => {
    let data = '';

    res.on('data', (chunk) => {
      data += chunk;
    });

    res.on('end', () => {
      try {
        results.push(JSON.parse(data));
      } catch (e) {
        results.push(data);
      }
      completedRequests++;

      if (completedRequests === ids.length) {
        // 将结果写入 data.txt 文件
        fs.writeFileSync('data.txt', JSON.stringify(results, null, 2));
        console.log('所有请求已完成，结果已保存到 data.txt');
      }
    });
  }).on('error', (err) => {
    console.error(`请求 ${url} 时出错:`, err.message);
    results.push({ error: err.message, id });
    completedRequests++;

    if (completedRequests === ids.length) {
      fs.writeFileSync('data.txt', JSON.stringify(results, null, 2));
      console.log('所有请求已完成，结果已保存到 data.txt');
    }
  });
}

// 循环调用请求
ids.forEach(id => {
  makeRequest(id);
});
